package com.ruoyi.material.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.material.mapper.ReceiptMaterialInfoMapper;
import com.ruoyi.material.vo.WarehuseCountVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.material.mapper.WarehouseInfoMapper;
import com.ruoyi.material.domain.WarehouseInfo;
import com.ruoyi.material.service.IWarehouseInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-26
 */
@Service
public class WarehouseInfoServiceImpl implements IWarehouseInfoService {
    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;

    @Autowired
    private ReceiptMaterialInfoMapper receiptMaterialInfoMapper;

    /**
     * 查询库存
     *
     * @param id 库存主键
     * @return 库存
     */
    @Override
    public WarehouseInfo selectWarehouseInfoById(Long id) {
        return warehouseInfoMapper.selectWarehouseInfoById(id);
    }

    /**
     * 查询库存列表
     *
     * @param warehouseInfo 库存
     * @return 库存
     */
    @Override
    public List<WarehouseInfo> selectWarehouseInfoList(WarehouseInfo warehouseInfo) {
        List<WarehouseInfo> warehouseInfos = new ArrayList<>();
        for (WarehouseInfo info : warehouseInfoMapper.selectWarehouseInfoList(warehouseInfo)) {
            if (info.getRemainderCount() < info.getMinCount()){
                //red
                info.setStockColor("D7001CFF");
            }else if(info.getRemainderCount() > info.getMaxCount()) {
                info.setStockColor("FCFD01FF");
            }
            warehouseInfos.add(info);
        }
        return warehouseInfos;
    }

    /**
     * 新增库存
     *
     * @param warehouseInfo 库存
     * @return 结果
     */
    @Override
    public int insertWarehouseInfo(WarehouseInfo warehouseInfo) {
        warehouseInfo.setCreateTime(DateUtils.getNowDate());
        return warehouseInfoMapper.insertWarehouseInfo(warehouseInfo);
    }

    /**
     * 修改库存
     *
     * @param warehouseInfo 库存
     * @return 结果
     */
    @Override
    public int updateWarehouseInfo(WarehouseInfo warehouseInfo) {
        //报溢数量，报损数量
        //如果未绑定项目，将期初库存加到库存数量；绑定项目不维护期初库存等，只能维护报溢数量和报损数量
        WarehouseInfo warehouseInfoDb = warehouseInfoMapper.selectWarehouseInfoById(warehouseInfo.getId());
        if (StringUtils.isEmpty(warehouseInfoDb.getProjectNo())){
            //项目未绑定remainderCount
            warehouseInfo.setRemainderCount(warehouseInfo.getRemainderCount() + warehouseInfo.getInitCount());
        }
        warehouseInfo.setRemainderCount(warehouseInfoDb.getRemainderCount()+warehouseInfo.getOverflowQuantity()-warehouseInfo.getLossQuantity());
        return warehouseInfoMapper.updateWarehouseInfo(warehouseInfo);
    }

    /**
     * 批量删除库存
     *
     * @param ids 需要删除的库存主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoByIds(Long[] ids) {
        return warehouseInfoMapper.deleteWarehouseInfoByIds(ids);
    }

    /**
     * 删除库存信息
     *
     * @param id 库存主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseInfoById(Long id) {
        return warehouseInfoMapper.deleteWarehouseInfoById(id);
    }

    //分配未绑定数量distribution
    @Override
    public AjaxResult distribution(WarehouseInfo warehouseInfo){
        //查询是否分配超
        WarehouseInfo warehouseInfoDb = warehouseInfoMapper.selectWarehouseInfoById(warehouseInfo.getId());
        long sum = warehouseInfo.getWarehuseCountS().stream().mapToLong(WarehuseCountVO::getCount).sum();
        if (sum > warehouseInfoDb.getRemainderCount()){
            return AjaxResult.error("分配数量大于仓库剩余量，分配失败！");
        }

        //维护查询类
        WarehouseInfo query = new WarehouseInfo();
        query.setMaterialName(warehouseInfo.getMaterialName());
        query.setMaterialType(warehouseInfo.getMaterialType());
        query.setSpecifications(warehouseInfo.getSpecifications());
        query.setUnit(warehouseInfo.getUnit());
        //物料id
        Long id = warehouseInfo.getId();
        for (WarehuseCountVO warehuseCount : warehouseInfo.getWarehuseCountS()) {
            //维护进入库存表
            //查询库存，确定物料是否存在
            query.setProjectNo(warehouseInfo.getProjectNo());
            Long aLong = warehouseInfoMapper.selectByQuery(query);
            if (aLong >0){
                //存在进行更新
                receiptMaterialInfoMapper.updateWarehouseInfo(aLong,warehuseCount.getCount());
            }else{
                //insert into warehouse_info(project_no,warehouse_name,warehouse_area,material_no,material_name,material_type,specifications,unit,distribution_count,remainder_count)
                WarehouseInfo info = new WarehouseInfo();
                info.setProjectNo(warehuseCount.getProjectNo());
                info.setWarehouseName(warehouseInfo.getWarehouseName());
                info.setWarehouseArea(warehouseInfo.getWarehouseArea());
                info.setMaterialName(warehouseInfo.getMaterialName());
                info.setMaterialType(warehouseInfo.getMaterialType());
                info.setSpecifications(warehouseInfo.getSpecifications());
                info.setUnit(warehouseInfo.getUnit());
                info.setDistributionCount(warehuseCount.getCount());
                info.setRemainderCount(warehuseCount.getCount());
                warehouseInfoMapper.insertWarehouseInfo(info);
            }
        }
        //减少分配数量
        warehouseInfoDb.setRemainderCount(warehouseInfoDb.getRemainderCount() - sum);
        warehouseInfoMapper.updateWarehouseInfo(warehouseInfoDb);
        return AjaxResult.success();
    }

    /**
     * 解绑已绑定数量
     * @param warehouseInfo
     * @return
     */
    @Override
    @Transactional
    public AjaxResult unbound(WarehouseInfo warehouseInfo){
        //物料id
        Long id = warehouseInfo.getId();
        long sum = warehouseInfo.getWarehuseCountS().stream().mapToLong(WarehuseCountVO::getCount).sum();
        WarehouseInfo warehouseInfoDb = warehouseInfoMapper.selectWarehouseInfoById(warehouseInfo.getId());

        if (warehouseInfo.getDisCount() > warehouseInfoDb.getRemainderCount()){
            return AjaxResult.error("解绑失败,解绑数量大于现有数量！");
        }
        warehouseInfoDb.setRemainderCount(warehouseInfoDb.getRemainderCount() - warehouseInfoDb.getDisCount());
        warehouseInfoDb.setDistributionCount(warehouseInfoDb.getDistributionCount() - warehouseInfo.getDisCount());
        warehouseInfoMapper.updateWarehouseInfo(warehouseInfoDb);


        //更新剩余量
        //维护查询类
        WarehouseInfo query = new WarehouseInfo();
        query.setMaterialName(warehouseInfo.getMaterialName());
        query.setMaterialType(warehouseInfo.getMaterialType());
        query.setSpecifications(warehouseInfo.getSpecifications());
        query.setUnit(warehouseInfo.getUnit());
        Long aLong = warehouseInfoMapper.selectByQuery(query);
        if (aLong >0) {
            //更新
            WarehouseInfo info = warehouseInfoMapper.selectWarehouseInfoById(aLong);
            info.setRemainderCount(info.getRemainderCount() + sum);
        }else {
            //插入
            query.setRemainderCount(sum);
            warehouseInfoMapper.insertWarehouseInfo(query);
        }
        return AjaxResult.success();
    }
}
